package telas;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import classes.Usuario;
import DAOs.DAOUsuario;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CadastraUsuario extends JFrame {

	private JPanel contentPane;
	private JTextField txtUsername;
	private JTextField txtPassword;
	private JButton btnCadastrar;
	private int status;
	private Usuario usuario = new Usuario();
	
	DAOUsuario dadosUsuario = new DAOUsuario();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CadastraUsuario frame = new CadastraUsuario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public void AbreTela(int status,Usuario usuario){
		this.status= status;
		this.usuario= usuario;
		if (status == 2) {
			txtUsername.setEditable(false);
			txtPassword.setText(usuario.getPassword());;
		}else{
			txtUsername.setEditable(true);
		}
	}
	public CadastraUsuario() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(55, 46, 77, 14);
		contentPane.add(lblUsername);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(55, 71, 150, 20);
		contentPane.add(txtUsername);
		txtUsername.setColumns(10);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(55, 148, 150, 20);
		contentPane.add(txtPassword);
		txtPassword.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(55, 123, 77, 14);
		contentPane.add(lblPassword);
		
		btnCadastrar = new JButton("Cadastrar");
		btnCadastrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				usuario.setUsername(txtUsername.getText());
				usuario.setPassword(txtPassword.getText());
				
				if(status == 1)
				dadosUsuario.createUsuario(usuario);
				else if (status ==2)
					dadosUsuario.updateUsuario(usuario);
			
				txtPassword.setText(null);
				txtUsername.setText(null);
				dispose();
			}
		});
		btnCadastrar.setBounds(57, 213, 89, 23);
		contentPane.add(btnCadastrar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtPassword.setText(null);
				txtUsername.setText(null);
				dispose();
			}
		});
		btnCancelar.setBounds(304, 213, 89, 23);
		contentPane.add(btnCancelar);
	}
}
